#include "MObject.h"


MObject::MObject(void):GObject()
{
	this->_objectType = 0;
	this->_status = 0;

	
	this->_isMovable = false;

	this->_canChangeVelocity = true;

	this->_isAnimating = false;
}

MObject::MObject(int x, int y, int Width, int Height, float vx, float vy, int deltaTime, LPD3DXSPRITE spriteHandler, LPDIRECT3DTEXTURE9 image):GObject(x,y,Width,Height)
{
	this->_vx = vx;
	this->_vy = vy;
	this->_deltaTime = deltaTime;

	this->_enablePushBackVector = false;

	int i = 0;

	this->_isMovable = false;
}

MObject::MObject(float x, float y, float vx, float vy, int deltaTime,int resourceName,CResourceManager *rm):GObject(x,y,resourceName,rm)
{
	this->_objectType = 0;
	switch (resourceName)
	{
	case CResourceManager::COIN:
		{
			this->_objectType = CResourceManager::TYPE_COIN;
			this->_scored = 100;
			break;
		}
	case CResourceManager::CHIMNEY:
		{
			this->_objectType = CResourceManager::TYPE_CHIMNEY3;
			break;
		}
	case CResourceManager::PIT:
		{
			this->_objectType = CResourceManager::TYPE_PIT;
			break;
		}
	case CResourceManager::STONE:
		{
			this->_objectType = CResourceManager::TYPE_STONE;
			break;
		}
	case CResourceManager::MIDLAND:
		{
			this->_objectType = CResourceManager::TYPE_MIDLAND;
			break;
		}
	}
	this->_status = 0;

	this->_vx = vx;
	this->_vy = vy;
	this->_deltaTime = deltaTime;
	this->_isMovable = false;

	this->_canChangeVelocity = true;

	this->_isAnimating = false;
}

MObject::MObject(float x, float y, float vx, float vy, int deltaTime,int resourceName):GObject(x,y,resourceName)
{
	this->_mainResourceName = resourceName;
	this->_objectType = 0;
	
	this->_status = 0;

	this->_vx = vx;
	this->_vy = vy;
	this->_deltaTime = deltaTime;
	this->_isMovable = false;

	this->_canChangeVelocity = true;

	this->_isAnimating = false;
}


Vector MObject::getVelocityVector()
{
	return Vector(_vx,_vy);
}

void MObject::setVelocity(Vector v, shared_ptr<MObject> Object)
{
	_vx = v.getX();
	_vy = v.getY();
}

MObject::~MObject(void)
{

//	//if (_rm)
//		delete _rm;
}

void MObject::Render()
{
	Vector v = Vector();
	v = CScreen::getInstance()->getPosition();

	this->_sprite->Render(_x - v.getX(),_y - v.getY());
}

void MObject::Move()
{
	_lastX = _x;
	_lastY = _y;

	_x += _vx*_deltaTime;
	_y += _vy*_deltaTime;

	
}

int MObject::getTranslateDirection(Vector translateVector)
{
	int x = translateVector.getX();
	int y = translateVector.getY();

	if (x == 0)
	{
		if (y<0)
			return 1;		//UP
		else if (y > 0)
			return 2;		//DOWN
	}
	else
	{
		if (y==0)
		{
			if (x<0)
				return 3;	//RIGHT
			else if (x>0) return 4;	//LEFT
		}
	}
	return -1;
}

void MObject::Translate(Vector v, shared_ptr<MObject> Object)
{

	if (abs(v.getX()) >= 2.0f)
		_x += v.getX();



	if (abs(v.getY()) >= 2.f)
		_y += v.getY();

	_lastTranslateVector = v;

	actAfterCollided(getTranslateDirection(_lastTranslateVector), Object);
}

void MObject::LoadResource(CResourceManager* rm)
{
	this->setResourceManager(rm);
	_sprite = rm->getSprite(_mainResourceName);
}